package com.cody.controller.admin;

import com.cody.entity.*;
import com.cody.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author Cody
 * @date 2021/4/8 - 20:37
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    private PassageService passageService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private SysService sysService;


    @RequestMapping(value = {"", "/"})
    public String login(){
        return "/admin/login";
    }
    @RequestMapping(value = {"/login"})
    public String index(@RequestParam(required = false, name = "username") String username, @RequestParam(required = false, name = "password") String password
            , HttpSession session
    ) {
        Sys sys = new Sys();
        sys = sysService.querySingleSys(username, password);
        session.setAttribute("user", sys);
        if (sys == null) {
            //用户名或者密码错误
            session.setAttribute("msg", "用户名或者密码错误");
            return "/admin/login";
        } else {
            return "redirect:/admin/index";
        }
    }

    @RequestMapping({"/index"})
    public String secondIndex(HttpSession session
    ) {
        List<Teacher> teachers;
        teachers = teacherService.queryAllTeacher();
        session.setAttribute("teachers", teachers);
        //所有老师
        List<Student> students;
        students = studentService.queryAllStudent();
        session.setAttribute("students", students);
        //所有问题
        List<Question> questions;
        questions = questionService.queryAllQuestionByAdmin();
        session.setAttribute("questions", questions);
        //所有文章
        List<Passage> passages;
        passages = passageService.queryAllPassage();
        session.setAttribute("passages", passages);
        //所有回答
        List<Answer> answers;
        answers = answerService.queryAllAnswer();
        session.setAttribute("answers", answers);
        //所有评论
        List<Comment> comments;
        comments = commentService.queryAllComment();
        session.setAttribute("comments", comments);
        List<Sys> list;
        list = sysService.queryAllSys();
        session.setAttribute("list", list);
        return "/admin/index";
    }

    @RequestMapping({"/logout"})
    public String index(HttpSession session
    ) {
        session.removeAttribute("user");
        return "redirect:/admin";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, String> del(@RequestParam(required = false, name = "adminId") int adminId) {
        Map<String, String> map = new HashMap<>();
        boolean flag = sysService.delSysByAdminId(adminId);
        if (flag) {
            map.put("msg", "删除成功");
        }
        return map;
    }

    @RequestMapping("/teacher/jump")
    public String jump01() {
        return "/admin/teacherSet";
    }

    @RequestMapping("/student/jump")
    public String jump() {
        return "/admin/studentSet";
    }

    @RequestMapping("/student/insert")
    public String register(@RequestParam(required = false, name = "userNumber") String username,
                           @RequestParam(required = false, name = "username") String userNumber,
                           @RequestParam(required = false, name = "password") String password,
                           @RequestParam(required = false, name = "rePassword") String rePassword,
                           @RequestParam(required = false, name = "sex") Integer sex,
                           @RequestParam(required = false, name = "phone") String phone,
                           @RequestParam(required = false, name = "studentSpecialty") String studentSpecialty,
                           @RequestParam(required = false, name = "studentClass") String studentClass, HttpSession session
    ) {
        if (!password.equals(rePassword)) {
            session.setAttribute("msgerror", "两次密码输入不一致！");
            return "/admin/studentSet";
        } else {
            Student student = new Student(1, username, userNumber, sex, password, studentSpecialty, studentClass, phone);
            studentService.insertSingleStudent(student);
        }
        return "/admin/welcome";

    }

    @RequestMapping("/teacher/insert")
    public String registerTeacher(@RequestParam(required = false, name = "userNumber") String username,
                                  @RequestParam(required = false, name = "username") String userNumber,
                                  @RequestParam(required = false, name = "password") String password,
                                  @RequestParam(required = false, name = "rePassword") String rePassword,
                                  @RequestParam(required = false, name = "sex") Integer sex,
                                  @RequestParam(required = false, name = "email") String email,
                                  @RequestParam(required = false, name = "teacherBrief") String teacherBrief,
                                  @RequestParam(required = false, name = "teacherPost") String teacherPost,
                                  @RequestParam(required = false, name = "teacherSubject") String teacherSubject,
                                  @RequestParam(required = false, name = "teacherDegree") String teacherDegree,
                                  HttpSession session
    ) {

        if (!password.equals(rePassword)) {
            session.setAttribute("msgerror", "两次密码输入不一致！");
            return "/admin/teacherSet";
        } else {
            Teacher teacher = new Teacher(1, teacherBrief, username, userNumber, sex, password, teacherPost, teacherSubject, teacherDegree, email);
            teacherService.insertSingleTeacher(teacher);
        }
        return "/admin/welcome";

    }


    @RequestMapping("/test")
    public String test() {
        return "redirect:/admin/teacher/insert";
    }

    @RequestMapping("/student/update")
    public String update(@RequestParam(required = false, name = "username") String username,
                         @RequestParam(required = false, name = "userNumber") String userNumber,
                         @RequestParam(required = false, name = "phone") String phone,
                         @RequestParam(required = false, name = "sex") Integer sex,
                         @RequestParam(required = false, name = "studentSpecialty") String studentSpecialty,
                         @RequestParam(required = false, name = "studentClass") String studentClass,
                         HttpSession session) {

        Student student = new Student(1, userNumber, username, sex, "1", studentSpecialty, studentClass, phone);
        studentService.updateStudentInfo(student);
        return "redirect:/admin/index";
    }

    @RequestMapping("/teacher/update")
    public String update(@RequestParam(required = false, name = "username") String username,
                         @RequestParam(required = false, name = "userNumber") String userNumber,
                         @RequestParam(required = false, name = "email") String email,
                         @RequestParam(required = false, name = "sex") Integer sex,
                         @RequestParam(required = false, name = "teacherBrief") String teacherBrief,
                         @RequestParam(required = false, name = "teacherPost") String teacherPost,
                         @RequestParam(required = false, name = "teacherSubject") String teacherSubject,
                         @RequestParam(required = false, name = "teacherDegree") String teacherDegree) {
        Teacher teacher = new Teacher(1, teacherBrief, userNumber, username, sex, "1", teacherPost, teacherSubject, teacherDegree, email);
        teacherService.updateTeacherInfo(teacher);
        return "redirect:/admin/index";
    }


    @RequestMapping("/student/search")
    @ResponseBody
    public List<Student> search(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据学生姓名进行搜索
        List<Student> studentList;
        studentList = studentService.queryStudentByKeyword("%" + keyContent + "%");
        return studentList;
    }

    @RequestMapping("/answer/search")
    @ResponseBody
    public List<Answer> answerSearch(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据回答内容进行搜索
        List<Answer> answerList;
        answerList = answerService.queryAnswerByKeyword("%" + keyContent + "%");
        return answerList;
    }

    @RequestMapping("/teacher/search")
    @ResponseBody
    public List<Teacher> teacherSearch(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据回答内容进行搜索
        List<Teacher> teacherList;
        teacherList = teacherService.queryTeacherByKeyword("%" + keyContent + "%");
        return teacherList;
    }

    @RequestMapping("/comment/search")
    @ResponseBody
    public List<Comment> commentSearch(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据回答内容进行搜索
        List<Comment> comments;
        comments = commentService.queryCommentByKeyword("%" + keyContent + "%");
        return comments;
    }

    @RequestMapping("/question/search")
    @ResponseBody
    public List<Question> questionSearch(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据回答内容进行搜索
        List<Question> questionList;
        questionList = questionService.queryQuestionByKeyword("%" + keyContent + "%");
        return questionList;
    }

    @RequestMapping("/passage/search")
    @ResponseBody
    public List<Passage> passageSearch(@RequestParam(required = false, name = "keyContent") String keyContent) {
        //关键字根据回答内容进行搜索
        List<Passage> passageList;
        passageList = passageService.queryCommentByKeyword("%" + keyContent + "%");
        return passageList;
    }

    @RequestMapping("/updTeacher/{teacherNumber}")
    public String updTeacher(@PathVariable("teacherNumber") String teacherNumber, HttpSession session) {
        //关键字根据回答内容进行搜索
        Teacher teacher = teacherService.querySingleTeacherByTeacherNumber(teacherNumber);
        session.setAttribute("teacherInfo", teacher);
        return "/admin/teacherUpd";
    }

    @RequestMapping("/admin/teacher/update")
    public String teacherUpdate(@RequestParam(required = false, name = "username") String username,
                                @RequestParam(required = false, name = "userNumber") String userNumber,
                                @RequestParam(required = false, name = "email") String email,
                                @RequestParam(required = false, name = "teacherBrief") String teacherBrief,
                                @RequestParam(required = false, name = "teacherPost") String teacherPost,
                                @RequestParam(required = false, name = "teacherSubject") String teacherSubject,
                                @RequestParam(required = false, name = "teacherDegree") String teacherDegree) {
        Teacher teacher = new Teacher(1, teacherBrief, userNumber, username, 1, "1", teacherPost, teacherSubject, teacherDegree, email);
        teacherService.updateTeacherInfo(teacher);
        return "redirect:/admin/index";
    }


    @RequestMapping("/updStudent/{studentNumber}")
    public String updStudent(@PathVariable("studentNumber") String studentNumber, HttpSession session) {
        //关键字根据回答内容进行搜索
        Student student = studentService.querySingleStudentByStudentNumber(studentNumber);
        session.setAttribute("studentInfo", student);
        return "/admin/studentUpd";
    }

    @RequestMapping("/admin/student/update")
    public String studentUpdate(@RequestParam(required = false, name = "username") String username,
                                @RequestParam(required = false, name = "userNumber") String userNumber,
                                @RequestParam(required = false, name = "phone") String phone,
                                @RequestParam(required = false, name = "studentSpecialty") String studentSpecialty,
                                @RequestParam(required = false, name = "studentClass") String studentClass) {

        Student student = new Student(1, userNumber, username, 1, "1", studentSpecialty, studentClass, phone);
        studentService.updateStudentInfo(student);
        return "redirect:/admin/index";
    }

    @RequestMapping("/getData")
    @ResponseBody
    public List<Result> getData() {
        List<Result> results = new ArrayList<>();
        List<HashMap<String, Object>> list = questionService.queryQuestion();
        for (int i = 0; i < list.size(); i++) {
            String x = (String) list.get(i).get("type");
            Long  y =(Long )list.get(i).get("count");
            results.add(new Result(x, y));
        }
        return results;
    }
}
